package com.lesserpand.springcloudalibabacore.config.redis.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author bailuo
 * @version 2.0
 * @Description redis key 失效监听
 * @Date Created in 2019/1/16 21:21
 */
@Component
public class RedisKeyTimeOutListener extends KeyExpirationEventMessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisKeyTimeOutListener.class);

    public RedisKeyTimeOutListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();
        String key = new String(body);
        if (key.contains("skill:")) {
            // 为了监听抢购商品过期
            LOGGER.info("监听到key = " + message + "的缓存数据过期失效了！");
        }
    }


}
